package cn.ryh.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

/**
 * @author Rao
 * @Description
 * @create 2022-01-13 16:39
 */
@RestController
public class SentinelResourceController  {

    @GetMapping("/onlyWithValue")
    @SentinelResource(value = "onlyWithValue",exceptionsToIgnore={Exception.class,IndexOutOfBoundsException.class,RuntimeException.class})
    public String onlyWithValue() {
        int i =10/0;
        return "blockHandler";
    }

    @GetMapping("/onlyWithblockHandler")
    @SentinelResource(value = "onlyWithblockHandler", blockHandler = "onlyWithblockHandler")
    public String onlyWithblockHandler() {
        int i =10/0;
        return "onlyWithblockHandler";
    }

    public String onlyWithblockHandler(BlockException block) {
        return "onlyWithblockHandler" + block.getMessage();
    }

    @GetMapping("/onlyWithblockHandlerClass")
    @SentinelResource(value = "onlyWithblockHandlerClass",
            blockHandler = "onlyWithblockHandlerClass",
            blockHandlerClass = BlockHandlerClass.class)
    public String onlyWithblockHandlerClass() {
        return "onlyWithblockHandlerClass";
    }

    @GetMapping("/onlyWithFallback")
    @SentinelResource(value = "onlyWithFallback", fallback = "onlyWithFallback2")
    public String onlyWithFallback() {
        return "onlyWithFallback";
    }

    public String onlyWithFallback2(Throwable throwable) {
        return "onlyWithFallba222ck" + throwable.getMessage();
    }

    @GetMapping("/onlyWithFallbackClass")
    @SentinelResource(value = "onlyWithFallbackClass",
            fallback = "onlyWithFallbackClass",
            fallbackClass = FallbackClass.class)
    public String onlyWithFallbackClass() {
        int i = 10/0;
        return "onlyWithFallbackClass";
    }
    @GetMapping("/blockHandlerWithFallback")
    @SentinelResource(value = "blockHandlerWithFallback",
            blockHandler = "blockHandlerWithFallback3",
            fallback = "blockHandlerWithFallback2")
    public String blockHandlerWithFallback() {
        int i = 10/0;
        return "blockHandler";
    }

    public String blockHandlerWithFallback3(BlockException block) {
        return "blockHandlerWithFallback BlockException " + block.getMessage();
    }

    public String blockHandlerWithFallback2(Throwable throwable) {
        return "blockHandlerWithFallback Throwable " + throwable.getMessage();
    }

    @GetMapping("/blockHandlerClassWithFallbackClass")
    @SentinelResource(value = "blockHandlerClassWithFallbackClass",
            blockHandler = "blockHandlerClassWithFallbackClass",
            blockHandlerClass = BlockHandlerClass.class,
            fallback = "blockHandlerClassWithFallbackClass",
            fallbackClass = FallbackClass.class,
            exceptionsToIgnore={Exception.class,IndexOutOfBoundsException.class,RuntimeException.class}
    )
    public String blockHandlerClassWithFallbackClass() {
        return "blockHandlerClassWithFallbackClass";
    }

}
